package com.macro.mall.wx.sdk;


import org.springframework.stereotype.Service;

import java.io.*;

/**
 * 微信支付类
 * @author zhouboyu
 * @Date 2020年8月25日
 */
@Service
public class WxMyConfig extends WXPayConfig{

    public final static String IOS_SCENE = "";

    private WxBeanConfig wxBeanConfig;

    public enum TRADE_TYPE{
        JSAPI("JSAPI"),
        NATIVE("NATIVE"),
        APP("APP"),//app
        MWEB("MWEB");//H5
        private String value;
        TRADE_TYPE(String value){
            this.value= value;
        }
        public String VALUE() {
            return value;
        }
    }


    private byte[] certData;
    @Override
    public String getAppID() {
        return wxBeanConfig.appId;
    }

    @Override
    public String getMchID() {
        return wxBeanConfig.mchId;
    }

    @Override
    public String getKey() {
        return wxBeanConfig.key;
    }

    @Override
    public String getNotifyUrl() {
        return wxBeanConfig.notifyUrl;
    }

    @Override
    InputStream getCertStream() {
        File file = new File(wxBeanConfig.certPath);
        InputStream stream = null;
        try {
            stream= new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return stream;
    }
    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }
    @Override
    IWXPayDomain getWXPayDomain() {
        return new IWXPayDomain() {
            @Override
            public void report(String domain, long elapsedTimeMillis, Exception ex) {
            }

            @Override
            public DomainInfo getDomain(WXPayConfig config) {
                return new DomainInfo("api.mch.weixin.qq.com", false);
            }
        };
    }
    public WxMyConfig(WxBeanConfig wxBeanConfig){
        this.wxBeanConfig = wxBeanConfig;
    }
    /**
     * 实现单例
     * @return
     */
    public static WxMyConfig getInstance(WxBeanConfig bean){
        return ConfigFactory.getWxMyConfig(bean);
    }
    /**
     * 内部类
     */
    public  static class ConfigFactory{
        private static WxMyConfig INSTANCE ;
        public static WxMyConfig getWxMyConfig(WxBeanConfig bean){
            INSTANCE = new WxMyConfig(bean);
            return INSTANCE;
        }
    }
}
